package com.hckjsec.cloud.controller;

import com.hckjsec.cloud.domain.WebApplicationConfig;
import com.hckjsec.cloud.domain.WebApplicationNews;
import com.hckjsec.cloud.domain.WebApplicationProduct;
import com.hckjsec.cloud.service.IWebApplicationConfigService;
import com.hckjsec.cloud.service.IWebApplicationNewsService;
import com.hckjsec.cloud.service.IWebApplicationProductService;
import com.hckjsec.common.core.domain.AjaxResult;
import com.hckjsec.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 此类处理网站端请求的接口数据
 */
@RestController
@RequestMapping("/webApi")
public class WebPublicApiController {
    @Autowired
    private IWebApplicationConfigService webApplicationConfigService;

    @Autowired
    private IWebApplicationProductService webApplicationProductService;

    @Autowired
    private IWebApplicationNewsService webApplicationNewsService;

    //网站初始化
    @GetMapping("/webinfo")
    public AjaxResult webinfo(HttpServletRequest req)
    {
        String appid = StringUtils.isHcNull(req.getParameter("appid"),"");
        if("".equals(appid)){
            return AjaxResult.cloud_602("参数不合法");
        }
        WebApplicationConfig  webApp = new WebApplicationConfig();
        webApp.setAppid(appid);
        List<WebApplicationConfig> webApplicationConfigList=webApplicationConfigService.selectWebApplicationConfigList(webApp);
        if (StringUtils.isEmpty(webApplicationConfigList) ){
            return AjaxResult.cloud_602("未查询到相关信息!");
        }
        webApp=webApplicationConfigList.get(0);
        return AjaxResult.success(webApp);
    }


    //产品信息
    @GetMapping("/productinfo")
    public AjaxResult productinfo(HttpServletRequest req)
    {
        String appid = StringUtils.isHcNull(req.getParameter("appid"),"");
        if("".equals(appid)){
            return AjaxResult.cloud_602("参数不合法");
        }
        WebApplicationProduct webApplicationProduct = new WebApplicationProduct();
        webApplicationProduct.setAppid(appid);
        webApplicationProduct.setState("true");
        List<WebApplicationProduct> list = webApplicationProductService.selectWebApplicationProductList(webApplicationProduct);
        list.forEach(kv->{
            kv.setContent(null);
        });
        if (StringUtils.isEmpty(list) ){
            return AjaxResult.cloud_602("未查询到相关信息!");
        }
        return AjaxResult.success(list);
    }

    //产品详细信息
    @GetMapping("/productinfoDetails")
    public AjaxResult productinfoDetails(HttpServletRequest req)
    {
        String id = StringUtils.isHcNull(req.getParameter("id"),"");
        if("".equals(id)){
            return AjaxResult.cloud_602("参数不合法");
        }
        return AjaxResult.success(webApplicationProductService.selectWebApplicationProductById(Long.valueOf(id)));
    }


    //新闻信息
    @GetMapping("/newsinfo")
    public AjaxResult newsinfo(HttpServletRequest req)
    {
        String appid = StringUtils.isHcNull(req.getParameter("appid"),"");
        if("".equals(appid)){
            return AjaxResult.cloud_602("参数不合法");
        }
        WebApplicationNews  webApplicationNews = new WebApplicationNews();
        webApplicationNews.setAppid(appid);
        webApplicationNews.setState("true");
        List<WebApplicationNews> list = webApplicationNewsService.selectWebApplicationNewsList(webApplicationNews);
        list.forEach(kv->{
            kv.setContent(null);
        });
        if (StringUtils.isEmpty(list) ){
            return AjaxResult.cloud_602("未查询到相关信息!");
        }
        return AjaxResult.success(list);
    }


    //新闻详细信息
    @GetMapping("/newsinfoDetails")
    public AjaxResult newsinfoDetails(HttpServletRequest req)
    {
        String id = StringUtils.isHcNull(req.getParameter("id"),"");
        if("".equals(id)){
            return AjaxResult.cloud_602("参数不合法");
        }
        return AjaxResult.success(webApplicationNewsService.selectWebApplicationNewsById(Long.valueOf(id)));
    }
}
